// pages/shelf_map/shelf_map.js
const $ = require('../../utils/utils');
const config = require('../../config.js')
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */

    data: {
        shelfList: [],
        pagesize: 20,
        pageno: 1,
        hasMoreData: true,
        scrollHeight:0,
        map_type: "0",
        shelf_code: "",
        area: "",
        isSelected: "1111",
        user_longitude: '',
        user_latitude: '',
        markers: [],
        isDrogUp: true,
        drogUrl: "../../img/drogDown.png",
        listScrollClass: "listScrollSelect",
        mapClass: "mapSelect"

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(`#shelf_map_onLoad()${options}`)
        var that = this;
        if (wx.getStorageSync('current_longitude') && wx.getStorageSync('current_latitude')){
            that.setData({
                user_longitude: wx.getStorageSync('current_longitude'),
                user_latitude: wx.getStorageSync('current_latitude')
            })
        }else{
            that.setData({
                user_longitude: '121.442000',
                user_latitude: '31.156000'
            })
        }
        that.getShelfList();
    },

    bindDownLoad: function () {
        var that = this;
        if (that.data.hasMoreData) {
            that.setData({
                pageno: that.data.pageno + 1
            })
            that.getShelfList();
        } else {
            wx.showToast({
                title: '没有更多数据',
            });
            setTimeout(function () {
                wx.hideToast();
            }, 2000);
        }
    },
    scroll: function (event) {
        this.setData({
            scrollHeight: event.detail.scrollHeight 
        });
    },  


    /******** Methods ***********/
    getShelfList:function(){
        let params = {
            pagesize: this.data.pagesize,
            pageno: this.data.pageno,
            user_longitude: wx.getStorageSync('current_longitude'),
            user_latitude: wx.getStorageSync('current_latitude'),
            map_type: this.data.map_type
        }
        let url = $.getUrl(config.queryshelfList, params)
        wx.showLoading({
            title: '加载中...',
            mask: true,
        })
        $.get(url).then(data => {
            wx.hideLoading()
            if (data.result == 1) {
                var contentlistTem = this.data.shelfList;
                var contentlist = data.items;
                if (contentlist.length < this.data.pagesize) {
                    this.setData({
                        shelfList: contentlistTem.concat(contentlist),
                        hasMoreData: false
                    })
                } else {
                    this.setData({
                        shelfList: contentlistTem.concat(contentlist),
                        hasMoreData: true,
                    });
                }


                let array = Array.from(this.data.shelfList)
                let markers = array.map(x => {
                    x.id = x.bookshelf_code
                    x.iconPath = '../../img/markerIcon.png'
                    return x
                })
                this.setData({
                    markers: markers
                });
            } else {
                wx.hideLoading()
                wx.showToast({
                    title: data.message,
                    duration: 2000,
                    icon: 'none'
                });
            }
        })
    },

    selectShelfCode: function (e) {
        var that = this;
        var code_area = e.currentTarget.dataset.shelfcode;
        var code = code_area.substring(0, 10);
        var area = code_area.substring(11);
        that.setData({
            shelf_code: code,
            area: area,
            isSelected: e.currentTarget.dataset.index
        });
        that.confirm();
    },

    tapDrogUp: function () {
        if (this.data.isDrogUp) {
            this.setData({
                isDrogUp: false,
                drogUrl: "../../img/drogUp.png",
                listScrollClass: "listScrollNormal",
                mapClass: "mapNormal"
            })
        } else {
            this.setData({
                isDrogUp: true,
                drogUrl: "../../img/drogDown.png",
                listScrollClass: "listScrollSelect",
                mapClass: "mapSelect"
            })
        }
    },



    confirm: function () {
        var that = this;
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        var prevPage = pages[pages.length - 2];
        if (currPage.data.shelf_code != '') {
            prevPage.setData({
                bookShelfCode: currPage.data.shelf_code,
                area: currPage.data.area,
                prevPageFlag: 1
            });
            wx.setStorageSync('bookShelfCode', currPage.data.shelf_code);
            wx.setStorageSync('area', currPage.data.area);
        } else {
            prevPage.setData({
                bookShelfCode: wx.getStorageSync('bookShelfCode'),
                area: wx.getStorageSync('area'),
                prevPageFlag: 1
            });
        }
        wx.setStorageSync('isBackFromShelfList', true)
        wx.navigateBack({
        })
    },

})